package com.mk.springbootvuedemo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mk.springbootvuedemo.config.Result;
import com.mk.springbootvuedemo.entity.User;
import com.mk.springbootvuedemo.mapper.UserMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

//定义返回JSON对象的controller
@RestController
//接口统一的路由
@RequestMapping("/user")
public class UserController {
    @Resource
    UserMapper userMapper;

    //    定义post接口,post是新增
    @PostMapping("/insert")
    //    @RequestBody把JOSN对象转换成JAVA对象
    public Result<?> save(@RequestBody User user) {
//        应该写一个service类，在controller引入service,setvice引入mapper
        if (user.getPassword() == null) {
            user.setPassword("123456");
        }
        userMapper.insert(user);
        return Result.success();
    }

    //登录
    @PostMapping("/login")
    public Result<?> login(@RequestBody User user) {
        User res = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername()).eq(User::getPassword, user.getPassword()));
        if (res == null) {
            return Result.error("-1", "用户名或密码错误");
        }
        return Result.success(res);
    }

    //注册
    @PostMapping("/register")
    public Result<?> register(@RequestBody User user) {
        User res = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername()));
        if (res != null) {
            return Result.error("-1", "用户名重复");
        }
        if (user.getPassword() == null) {
            user.setPassword("123456");
        }
        userMapper.insert(user);
        return Result.success();
    }

    //Delete是删除
    @DeleteMapping("/{id}")//占位符的方式要通过@PathhVariable获取
    public Result<?> update(@PathVariable Long id) {
        userMapper.deleteById(id);
        return Result.success();
    }

    //Put是更新
    @PutMapping("/update")
    public Result<?> update(@RequestBody User user) {
        userMapper.updateById(user);
        return Result.success();
    }

    //通过GetMapping在前端通过id查找用户信息，确认当前登录用户的合法信息
    @GetMapping("/{id}")//占位符的方式要通过@PathhVariable获取
    public Result<?> getById(@PathVariable Long id) {
        return Result.success(userMapper.selectById(id));
    }
    //Get是查询
    @GetMapping("/search")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<User> wrapper=Wrappers.<User>lambdaQuery();
//        避免nickName没有数据时查不出来
        if(StrUtil.isNotBlank(search)){
            wrapper.like(User::getNickname,search);
        }
        Page<User> userPage= userMapper.findPage(new Page<>(pageNum,pageSize));
        return Result.success(userPage);

    }

}
